package com.glory.dgcpcpda.activity.mfg;

import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.adapter.AddCarrierDataAdapter;
import com.glory.dgcpcpda.adapter.DefectListAdapter;
import com.glory.dgcpcpda.adapter.StripIdListAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.base.MyApp;
import com.glory.dgcpcpda.entity.AdrefListEntity;
import com.glory.dgcpcpda.entity.DefectCodeEntity;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.LotList;
import com.glory.dgcpcpda.entity.ScrapEntity;
import com.glory.dgcpcpda.entity.StripMappingEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * FileName: WBWorkEndActivity
 * Author: XJH
 * Date: 2020/5/21 11:43
 * Description:WB工序结束作业
 * History:
 */
public class WBWorkEndActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvLotState)
    TextView tvLotState;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.etEqpWorkID)
    FreeUI_ClearEditText etEqpWorkID;
    @BindView(R.id.tvStep)
    TextView tvStep;
    @BindView(R.id.etTrackOutNum)
    EditText etTrackOutNum;
    @BindView(R.id.etProjectNum)
    FreeUI_ClearEditText etProjectNum;
    @BindView(R.id.etComment)
    FreeUI_ClearEditText etComment;
    @BindView(R.id.etMaterielModel)
    FreeUI_ClearEditText etMaterielModel;
    @BindView(R.id.tvMaterielModel)
    TextView tvMaterielModel;
    @BindView(R.id.rvMaterielModelList)
    RecyclerView rvMaterielModelList;
    @BindView(R.id.llBoxLayout)
    LinearLayout llBoxLayout;
    @BindView(R.id.etMaterielBox)
    FreeUI_ClearEditText etMaterielBox;
    @BindView(R.id.rvMaterielBoxList)
    RecyclerView rvMaterielBoxList;
    @BindView(R.id.tvDefectCode)
    TextView tvDefectCode;
    @BindView(R.id.ivDefectArrow)
    ImageView ivDefectArrow;
    @BindView(R.id.flDefectList)
    FrameLayout flDefectList;
    @BindView(R.id.rvDefectList)
    RecyclerView rvDefectList;
    @BindView(R.id.tvDefectNum)
    TextView tvDefectNum;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etUserName)
    FreeUI_ClearEditText etUserName;
    @BindView(R.id.rvPcsQtyList)
    RecyclerView rvPcsQtyList;
    @BindView(R.id.tvAdd)
    TextView tvAdd;
    @BindView(R.id.etPcs)
    FreeUI_ClearEditText etPcs;
    @BindView(R.id.etQty)
    FreeUI_ClearEditText etQty;
    @BindView(R.id.etProjectPcs)
    LinearLayout etProjectPcs;
    @BindView(R.id.etProjectQty)
    LinearLayout etProjectQty;
    @BindView(R.id.rvStripIdList)
    RecyclerView rvStripIdList;

    private List<String> defectCodeList;
    private FreeUI_GeneralPop codePop;
    private DefectListAdapter defectListAdapter;
    private LotInfoEntity lotInfoEntity;
    private AddCarrierDataAdapter materielModelAdapter;
    private AddCarrierDataAdapter materielBoxAdapter;
    private StripIdListAdapter suggestListAdapter;
    private String equipmentid;
    private List<String> mDurablelist;
    private String needEqp;
    private String type;
    private AddCarrierDataAdapter mPcsQtyAdapter;
    private int ProjectNum=0;//MES返回框架条数
    private int isStripID=0;//判断是否校验领班权限
    private  ArrayList<String> StepNameList1 = new ArrayList<>();//不允许进站工序列表
    private  ArrayList<String> XrayStepNameList = new ArrayList<>();
    private String tdcmsg;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_work_end_wb;
    }

    @Override
    protected void initView() {
        tvTitle.setText("结束作业");
        setMainLayout(llMain);

        initRecyclerView(rvStripIdList,this);
        suggestListAdapter = new StripIdListAdapter(null);
        suggestListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        suggestListAdapter.isFirstOnly(true);
        rvStripIdList.setAdapter(suggestListAdapter);
        clear();

//        etProject.setVisibility(View.GONE);
        defectCodeList = new ArrayList<>();
        initRecyclerView(rvDefectList, this);
        defectListAdapter = new DefectListAdapter(null, tvDefectNum);
        defectListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        defectListAdapter.isFirstOnly(true);
        rvDefectList.setAdapter(defectListAdapter);

        initRecyclerView(rvMaterielModelList, this);
        initRecyclerView(rvMaterielBoxList, this);
        llBoxLayout.setVisibility(View.GONE);
        tvMaterielModel.setText("料框/料盒：");
        etMaterielModel.setHint("请扫描料框/料盒");
        initRecyclerView(rvPcsQtyList, this);
        materielModelAdapter = getCarrierAdapter();
        rvMaterielModelList.setAdapter(materielModelAdapter);
        materielBoxAdapter = getCarrierAdapter();
        rvMaterielBoxList.setAdapter(materielBoxAdapter);
        mPcsQtyAdapter = getCarrierAdapter();
        rvPcsQtyList.setAdapter(mPcsQtyAdapter);

        showCarrierLayout();//获取是否进行X-RAY检验
        showCarrierLayout1();//获取不允许在此功能进行操作工序

        needEqp="YES";
        type="DefaultTrackOut";
        initEdit(etLot, s -> {
            //获取lot信息
            searchLot(s);
        });

        initEdit(etEqpWorkID,s ->{
            getStripMappingData(s);
        });

        initEdit(etMaterielModel, s -> {
            if (mDurablelist == null){
                showToast("请先扫描批次号!", false);
                return;
            }

            if (materielModelAdapter.getData().contains(s)) {
                showToast("当前载具已添加到载具列表中", false);
                return;
            }
            LotUtils.checkCarrier(WBWorkEndActivity.this,getText(etLot),s, getSupportFragmentManager(), new LotUtils.netCallback<Object>() {

                @Override
                public void onSuccess(Object response) {
                    materielModelAdapter.addData(s);
                    etMaterielModel.setText("");
                }

                @Override
                public void onFail(Throwable t) {
                    loadError(t, "CHECKCARRIER");
                }
            });
        });

        initEdit(etMaterielBox, s -> {
            if (mDurablelist == null){
                showToast("请先扫描批次号!", false);
                return;
            }

            if (materielBoxAdapter.getData().contains(s)) {
                showToast("当前载具已添加到载具列表中", false);
                return;
            }
            LotUtils.checkCarrier(WBWorkEndActivity.this,getText(etLot),s, getSupportFragmentManager(), new LotUtils.netCallback<Object>() {

                @Override
                public void onSuccess(Object response) {
                    materielBoxAdapter.addData(s);
                    etMaterielBox.setText("");
                }

                @Override
                public void onFail(Throwable t) {
                    loadError(t, "CHECKCARRIER");
                }
            });
        });

        etTrackOutNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                defectListAdapter.setTotalNum(Double.parseDouble(editable.toString()));
            }
        });

    }

    private void searchLot(String s) {
        LotUtils.searchLotInfo(WBWorkEndActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
            @Override
            public void onSuccess(LotInfoEntity response) {
                lotInfoEntity = response;
                if (StepNameList1.contains(response.getLOT().getSTEPNAME())){
                    FreeUI_GeneralFragmentDialog tipDialog = FreeApi_DialogUtils.getConfirmDialog(MyApp.app, "确定", "当前工序:"+response.getLOT().getSTEPDESC()+"("+response.getLOT().getSTEPNAME()+")不允许在此功能进行操作", () -> {
                        finish();
                    });
                    tipDialog.show(getSupportFragmentManager(),"DIALOG");
                    return;
                }
                equipmentid = lotInfoEntity.getLOT().getEQUIPMENTID();
                //etEqpWorkID.setText(equipmentid);
                tvStep.setText(response.getLOT().getSTEPNAME() + "/" + response.getLOT().getSTEPDESC());
                etProjectNum.setText(String.valueOf(response.getPACKAGETYPEQTY()));
                ProjectNum=response.getPACKAGETYPEQTY();
                mDurablelist = response.getDURABLELIST();

                if(response.getISPOINTCHECK().equals("Y")){
                    etProjectPcs.setVisibility(View.VISIBLE);
                    etProjectQty.setVisibility(View.VISIBLE);
                }

//                if(!response.getLOT().getSTEPNAME().equals("A7000")) {
//                    materielModelAdapter.setNewData(mDurablelist);//注释载具
//                }
                tdcmsg="";
                if(response.getLOT().getSTEPNAME().equals("A7100")){
                    tdcmsg=response.getTDCMSG();
                    if("Y".equals(tdcmsg)){
                       // showToast("请注意，该批次塑封及塑封前工序有报废！", false);
                        showConfirmDialog("请注意，该批次塑封及塑封前工序有报废！");
                    }
                }
                    etTrackOutNum.setText(response.getLOT().getMAINQTY());

                String state = response.getLOT().getSTATE();
                if(state.equals("正在作业")){
                    state = state + " - " + response.getLOT().getEQUIPMENTID();
                }
                tvLotState.setText(state);
                if(StringUtils.isNotEmpty(response.getEQPNEEDED())){
                    if("1024107".equals(response.getEQPNEEDED())){
                        needEqp="NO";
                        etEqpWorkID.setHint("当前工序不需要设备");
                        etEqpWorkID.setEnabled(false);

                        etUserName.setFocusable(true);
                        etUserName.requestFocus();
                        etUserName.setFocusableInTouchMode(true);
                        etUserName.requestFocusFromTouch();

                    }else{
                        needEqp="YES";
                        etEqpWorkID.setHint("请扫描设备编号");
                        etEqpWorkID.setEnabled(true);

                        etEqpWorkID.setFocusable(true);
                        etEqpWorkID.requestFocus();
                        etEqpWorkID.setFocusableInTouchMode(true);
                        etEqpWorkID.requestFocusFromTouch();

                    }

                }
                getDefectInfo(s);
            }

            @Override
            public void onFail(Throwable t) {
                loadError(t, "LOTQUERY");
                showConfirmDialog(WBWorkEndActivity.this,"接口不通");
            }
        });
    }

    private void getDefectInfo(String s) {

        FreeUI_EntityCallBack<BaseResponseBean<DefectCodeEntity>> callback = new FreeUI_DialogEntityCallBack<BaseResponseBean<DefectCodeEntity>>
                (new TypeToken<BaseResponseBean<DefectCodeEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<DefectCodeEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    defectCodeList.clear();
                    List<DefectCodeEntity.DefectCode> scrapcodelists = response.body().getResponse().getBody().getSCRAPCODELISTS();
                    for (DefectCodeEntity.DefectCode code : scrapcodelists){
                        defectCodeList.add(code.getKEYID() + "/" + code.getDESCRIPTION());
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<DefectCodeEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"GETSTEPBYLOT");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", "BYLOT");
        map.add("LOT", new JsonParser().parse(new Gson().toJson(new LotList(s))).getAsJsonObject());//传批次集合
        NetUtils.requestPostNet("MES.GETSTEPBYLOT", "GETSTEPBYLOT", this, map, callback);
    }

    private void getStripMappingData(String eqpID) {
        FreeUI_EntityCallBack<BaseResponseBean<StripMappingEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<StripMappingEntity>>
                (new TypeToken<BaseResponseBean<StripMappingEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<StripMappingEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                    ArrayList<String> list=response.body().getResponse().getBody().getStripId();
                    ArrayList<StripMappingEntity.MESBINCODES> mesbincodeslist=response.body().getResponse().getBody().getStripBadCode();
                    clear();
                    if(mesbincodeslist.size()>0){
                        if (defectCodeList.size() <= 0) {
                            showToast("未获取到不良代码列表", false);
                            return;
                        }
                        defectListAdapter.setNewData(null);
                        int tvNum = 0;
                        for(int i=0 ;i<mesbincodeslist.size();i++){
                            for (String code : defectCodeList){
                                if(code.split("/")[0].equals(mesbincodeslist.get(i).getMESBINCODE())){
                                    ScrapEntity scrapEntity = new ScrapEntity();
                                    scrapEntity.setACTIONCODE(code.split("/")[0]);
                                    scrapEntity.setACTIONREASON(code.split("/")[1]);
                                    scrapEntity.setLOTID(lotInfoEntity.getLOT().getLOTID());
                                    scrapEntity.setLOTRRN(lotInfoEntity.getLOT().getOBJECTRRN());
                                    scrapEntity.setMAINQTY(mesbincodeslist.get(i).getNUMBER());
                                    ScrapEntity.MainQTY mainQTY = new ScrapEntity.MainQTY(mesbincodeslist.get(i).getNUMBER());
                                    List<ScrapEntity.MainQTY> data = new ArrayList<>();
                                    data.add(mainQTY);
                                    scrapEntity.setQTYLIST(data);
                                    scrapEntity.setACTIONTYPE("Scrap");
                                    defectListAdapter.addData(scrapEntity);
                                    tvNum = tvNum + Integer.parseInt(mesbincodeslist.get(i).getNUMBER());
                                }
                            }
                        }
                        tvDefectNum.setText(Integer.toString(tvNum));
                    }
                    if(response.body().getResponse().getHeader().getRESULTCODE().equals("408")){
                        suggestListAdapter.setNewData(list);
                        showConfirmDialog(response.body().getResponse().getHeader().getRESULTMESSAGE());
                        isStripID=1;
                        return;
                    }
                    //不对比StripID列表，直接带出结果
                    if(response.body().getResponse().getHeader().getRESULTCODE().equals("409")){
                        suggestListAdapter.setNewData(list);
                        isStripID=0;
                        return;
                    }
                    if(list.size()>0){
                        suggestListAdapter.setNewData(list);
                        showToast("StripId对比成功",true);
                        isStripID=0;
                        return;
                    }else {
                        isStripID=0;
                        return;
                    }
                }

            }

            @Override
            public void onError(Response<BaseResponseBean<StripMappingEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"GetStrimpMappingData");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("LOTID", getText(etLot));
        map.addProperty("CUSTOMERCODE", lotInfoEntity.getLOT().getCUSTOMERCODE());
        if(XrayStepNameList.contains(lotInfoEntity.getLOT().getSTEPNAME())){
            map.addProperty("ISXRAY", "1");
        }else {
            map.addProperty("ISXRAY", "0");
        }
        map.addProperty("EQUIPMENTID",eqpID);
        NetUtils.requestPostNet("EAP.GetStrimpMappingData","GetStrimpMappingData",WBWorkEndActivity.this,map,callBack);
    }

    //获取不允许进站工序
    private void showCarrierLayout1() {
        FreeUI_EntityCallBack<BaseResponseBean<AdrefListEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<AdrefListEntity>>
                (new TypeToken<BaseResponseBean<AdrefListEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<AdrefListEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                    List<AdrefListEntity.Adref> adreflist = response.body().getResponse().getBody().getADREFLIST();
                    StepNameList1.clear();
                    for (AdrefListEntity.Adref entity : adreflist) {
                        StepNameList1.add(entity.getKEY());
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<AdrefListEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"ADREFLISTQUERY");

            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("REFERENCETYPE","USER");
        map.addProperty("REFERENCENAME","DianDu__NoAllow_CheckInOut");
        NetUtils.requestPostNet("CPC.ADREFLISTQUERY","ADREFLISTQUERY",this,map,callBack);
    }

    /**
     * 是否显示载具
     */
    private void showCarrierLayout() {
        FreeUI_EntityCallBack<BaseResponseBean<AdrefListEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<AdrefListEntity>>
                (new TypeToken<BaseResponseBean<AdrefListEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<AdrefListEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                    List<AdrefListEntity.Adref> adreflist = response.body().getResponse().getBody().getADREFLIST();
                    XrayStepNameList.clear();
                    for (AdrefListEntity.Adref entity : adreflist) {
                        if(entity.getKEY().equals("X-RAY")){
                            XrayStepNameList.add(entity.getTEXT());
                        }
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<AdrefListEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"ADREFLISTQUERY");

            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("REFERENCETYPE","USER");
        map.addProperty("REFERENCENAME","CPC_CARRIERS_CHECK_STEP");
        NetUtils.requestPostNet("CPC.ADREFLISTQUERY","ADREFLISTQUERY",this,map,callBack);
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvBaseSubmit, R.id.flDefectList, R.id.tvAdd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                if (actionControl("Submit24")) {
                    return;
                }

                if(isStripID==1) {
                    //键合领班权限
                    if (actionControl1("Submit40")) {
                        return;
                    }
                }

                if (getText(etLot).equals("")){
                    showToast("请先扫描批次信息",false);
                    return;
                }

                if (getText(etUserName).equals("")){
                    showToast("请输入工号",false);
                    return;
                }

                if (tvLotState.getText().toString().equals("待投产")){
                    showConfirmDialog("批次未投产，请联系计划部执行投产");
                    return;
                }

                if (tvLotState.getText().toString().equals("等待作业")){
                    showConfirmDialog("批次没有作业，请操作进站功能");
                    return;
                }

                if (lotInfoEntity.getLOT().getSTEPDESC().contains("装片")){
                    showToast("装片工序请用[DA结束作业]出站",false);
                    return;
                }

                List<String> data = materielModelAdapter.getData();
                List<String> dataBox=materielBoxAdapter.getData();
                if (data.size() <= 0 && dataBox.size()<=0){
                   if( !lotInfoEntity.getLOT().getPARTNAME().startsWith("D")){
                       showToast("出站请绑定载具号",false);
                       return;
                   }

                }
                    if("YES".equals(needEqp)) {
                        type="DefaultTrackOut";
                        if (etEqpWorkID.getText().toString().equals("") ) {
                            showToast("设备编号不能为空！", false);
                            return;
                        }
                        if (!etEqpWorkID.getText().toString().equals(lotInfoEntity.getLOT().getEQUIPMENTID())) {
                            showToast("进出站设备不一致，不允许出站！", false);
                            return;
                        }
                    }else{
                        type="NoEqpTrackOut";
                    }
                int defectNum = 0;
                if (!tvDefectNum.getText().toString().equals("")){
                    defectNum = Integer.parseInt(tvDefectNum.getText().toString());
                }

                //List<String> dataBox = materielBoxAdapter.getData();
                data.addAll(dataBox);
                String qty = getText(etQty).equals("")?"0":getText(etQty);
                LotUtils.lotWBTrackOut(this, getSupportFragmentManager(),getText(etUserName),etEqpWorkID.getText().toString(),
                        getText(etLot), defectListAdapter.getData(), data, lotInfoEntity,
                        defectNum, "", type,mPcsQtyAdapter.getData(),Double.parseDouble(qty),new LotUtils.netCallback() {
                            @Override
                            public void onSuccess(Object response) {
                                ProjectNum=0;
                                isStripID=0;
                                finish(); // 把结束 交给这里
                            }
                            @Override
                            public void onFail(Throwable t) {
                                searchLot(getText(etLot));
                            }
                        });
                break;
            case R.id.tvAdd:
                if (lotInfoEntity.getCUTBASICQTY() == 0){
                    showToast("对应的切筋信息未找到或未维护数量，请联系切筋工艺维护数据",false);
                    return;
                }

                if (Integer.parseInt(getText(etPcs).equals("")?"0":getText(etPcs)) >= lotInfoEntity.getCUTBASICQTY()){
                    showToast("输入数量大于切筋数量，请重新输入",false);
                    etPcs.setText("");
                    etPcs.requestFocus();
                    return;
                }
                if (mPcsQtyAdapter.getData().size() == 5){
                    showToast("最多添加5条数据",false);
                    return;
                }
                if (getText(etPcs).equals("")){
                    showToast("尾管/盘(PCS)不能为空",false);
                    return;
                }

                mPcsQtyAdapter.addData(getText(etPcs));
                break;
            case R.id.flDefectList:
                if (defectCodeList.size() <= 0) {
                    showToast("未获取到不良代码列表", false);
                    return;
                }

                if (codePop != null) {
                    if (codePop.isShowing()) {
                        codePop.dismiss();
                    }
                } else {
                    codePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flDefectList.getWidth(), defectCodeList, ivDefectArrow, (codeS, i) -> {
                        tvDefectCode.setText(codeS.split("/")[0]);
                        ScrapEntity scrapEntity = new ScrapEntity();
                        scrapEntity.setACTIONCODE(codeS.split("/")[0]);
                        scrapEntity.setACTIONREASON(codeS.split("/")[1]);
                        scrapEntity.setLOTID(lotInfoEntity.getLOT().getLOTID());
                        scrapEntity.setLOTRRN(lotInfoEntity.getLOT().getOBJECTRRN());
                        scrapEntity.setACTIONTYPE("Scrap");

                        if (defectListAdapter.getData().size() > 0) {
                            if (containCode(scrapEntity)) {
                                defectListAdapter.addData(scrapEntity);
                            }
                        } else {
                            defectListAdapter.addData(scrapEntity);
                        }

                    });
                }
                FreeApi_PopUtils.setArrowShow(ivDefectArrow);
                codePop.showAsDropDown(flDefectList, 0, 0);
                break;
        }
    }

    private boolean containCode(ScrapEntity scrapEntity) {
        for (ScrapEntity bean : defectListAdapter.getData()) {
            if (bean.getACTIONCODE().equals(scrapEntity.getACTIONCODE())) {
                showToast("列表中已有当前不良代码!", false);
                return false;
            }
        }
        return true;
    }

    private void clear(){
        suggestListAdapter.getData().clear();
        suggestListAdapter.notifyDataSetChanged();
    }
}